package com.youthburin.rmp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youthburin.base.controller.BaseController;
import com.youthburin.base.services.OpLogService;
import com.youthburin.base.util.DwzPagination;
import com.youthburin.base.util.DwzResponse;
import com.youthburin.base.util.DwzResponseUtils;
import com.youthburin.rmp.domain.RmpRole;
import com.youthburin.rmp.enums.RmpOpLogBusiTypeEnum;
import com.youthburin.rmp.enums.RmpOpLogTypeEnum;
import com.youthburin.rmp.services.RmpRoleService;

@Controller
public class RmpRoleController extends BaseController {
	@Autowired
	private RmpRoleService rmpRoleService;
	@Autowired
	@Qualifier("rmpOpLogService")
	private OpLogService opLogService;

	@RequiresPermissions("role:list")
	@RequestMapping(value = "/role/list")
	public String list(HttpServletRequest request) {
		opLogService.addLog(RmpOpLogTypeEnum.LIST.toString(), RmpOpLogBusiTypeEnum.RMP_ROLE.toString());
		rmpRoleService.dwzFindAll((DwzPagination) request.getAttribute(DwzPagination.DWZPAGINATION_PROPERTY_KEY));
		return "role/list";
	}

	@RequiresPermissions("role:add")
	@RequestMapping(value = "/role/add", method = RequestMethod.GET)
	public String toAdd(ModelMap model) {
		model.put("permissionGroup", rmpRoleService.findRmpPermissionGroups());
		model.put("userList", rmpRoleService.findRmpUsers());
		return "role/edit";
	}

	@RequiresPermissions("role:add")
	@RequestMapping(value = "/role/add", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse add(@Valid RmpRole rmpRole, BindingResult bindingResult, HttpServletRequest request) {
		if (bindingResult.hasErrors()) {
			return DwzResponseUtils.createFailResponse(bindingResult.getAllErrors());
		} else {
			String[] permissions = request.getParameterValues("permission");
			String[] userCodes = request.getParameterValues("userCode");
			RmpRole newRmpRole = rmpRoleService.save(rmpRole, permissions, userCodes);

			opLogService.addLog(RmpOpLogTypeEnum.ADD.toString(), RmpOpLogBusiTypeEnum.RMP_ROLE.toString(),
					newRmpRole.getCode(), newRmpRole.getName());
			return DwzResponseUtils.createSuccessResponse(true, "role_panel");
		}
	}

	@RequiresPermissions("role:edit")
	@RequestMapping(value = "/role/edit/{code}", method = RequestMethod.GET)
	public String toEdit(@PathVariable("code") String code, ModelMap model) {
		RmpRole role = rmpRoleService.get(code);
		model.put("role", role);
		model.put("permissionGroup", role.getRmpPermssionsGroups());
		model.put("userList", role.getAllUsers());
		return "role/edit";
	}

	@RequiresPermissions("role:view")
	@RequestMapping(value = "/role/view/{code}", method = RequestMethod.GET)
	public String toView(@PathVariable("code") String code, ModelMap model) {
		RmpRole role = rmpRoleService.get(code);
		opLogService.addLog(RmpOpLogTypeEnum.VIEW.toString(), RmpOpLogBusiTypeEnum.RMP_ROLE.toString(), code,
				role.getName());
		model.put("role", role);
		model.put("permissionGroup", role.getRmpPermssionsGroups());
		model.put("userList", role.getAllUsers());
		model.put("readOnly", "1");
		return "role/edit";
	}

	@RequiresPermissions("role:edit")
	@RequestMapping(value = "/role/edit", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse edit(RmpRole rmpRole, HttpServletRequest request) {
		String[] permissions = request.getParameterValues("permission");
		String[] userCodes = request.getParameterValues("userCode");

		RmpRole newRmpRole = rmpRoleService.update(rmpRole, permissions, userCodes);

		opLogService.addLog(RmpOpLogTypeEnum.UPDATE.toString(), RmpOpLogBusiTypeEnum.RMP_ROLE.toString(),
				newRmpRole.getCode(), newRmpRole.getName());
		return DwzResponseUtils.createSuccessResponse(true, "role_panel");
	}

	@RequiresPermissions("role:delete")
	@RequestMapping(value = "/role/delete", method = RequestMethod.POST)
	@ResponseBody
	public DwzResponse delete(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			List<String> idList = new ArrayList<String>();
			for (String id : ids.split(",")) {
				if (StringUtils.isNotBlank(id)) {
					idList.add(StringUtils.trim(id));
				}
			}
			rmpRoleService.delete(idList);

			for (String id : idList) {
				opLogService.addLog(RmpOpLogTypeEnum.DELETE.toString(), RmpOpLogBusiTypeEnum.RMP_ROLE.toString(), id);
			}
		}
		return DwzResponseUtils.createSuccessResponse(false);
	}

}
